/**
 * Filename : functions.c
 * Author   : Zhangjinze (799423351@qq.com)
 * Data     : 20131215
 */

#include "caipiao.h"
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

/**
 * OpenAFile(): Open a file....
 */
FILE* OpenAFile(char *FileName, char *OpenMethod)
{
    FILE *FileOpenP;
    //printf("*****Open file %s*****\n", FileName);
    if ((FileOpenP=fopen(FileName, OpenMethod)) == NULL) {
        printf("File %s open failed.\n", FileName);
        return NULL;
    }
    return FileOpenP;
}

/**
 * writeToFile(): Open a file....
 */
static int WriteToFile(FILE *WriteToFileP, int *ArrNum)
{
    // set the file pointer to the head.
    if ((fseek(WriteToFileP, 0L, 0)) != 0) {
        printf("Set file pointer to head error.\n");
        exit(1);
    }
    if (!fprintf(WriteToFileP, "%6d,%2d,%2d,%2d,%2d,%2d,%2d,%2d\n", ArrNum[0], ArrNum[1], ArrNum[2], ArrNum[3],
                                                                ArrNum[4], ArrNum[5], ArrNum[6], ArrNum[7])) {
        printf("Write stage:%d error!\n", ArrNum[0]);
        return 0;
    }
    return 1;
}

/**
 * ReadFromFile(): Read from a file....
 */
static struct OneStageNumbers* ReadOneLineFromFile(FILE *ReadOneLineFromFileP)
{
    struct OneStageNumbers ArrNum;
    char c;
    if (!fscanf(ReadOneLineFromFileP, "%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d\n", &ArrNum.Num[0], &c, &ArrNum.Num[1], &c, &ArrNum.Num[2], &c, &ArrNum.Num[3], &c,
                                                                        &ArrNum.Num[4], &c, &ArrNum.Num[5], &c, &ArrNum.Num[6], &c, &ArrNum.Num[7])) {
        printf("Read stage:%d error!\n", ArrNum.Num[0]);
        exit(1);
    }
    return &ArrNum;
}

/**
 * int *FilterLastStageNumber() : ling hao guolv
 */
int FilterLastStageNumber() // ling hao guolv
{
    int i, j, tempi, tempj, ArrNumSave[8], *ArrNearNumSave;
    struct OneStageNumbers* ArrNum;
    NearNumberList *p1, *p2, *p3;
    FILE *FileP;

    if ((FileP =  OpenAFile("LuckyNumbers.txt", "a+")) == NULL) {
        exit(1);
    }

    ArrNum = ReadOneLineFromFile(FileP);
    for(i=0; i<8; i++) {
        // Make a temp array is to save the data. Because after ReadOneLineFromFile function
        // is over, the memory of ArrNum is gone.
        ArrNumSave[i] = ArrNum->Num[i];
    }
    printf("The lastest stage numbers are:%d,%d,%d,%d,%d,%d\n", ArrNumSave[1], ArrNumSave[2], ArrNumSave[3]
                                                            , ArrNumSave[4], ArrNumSave[5], ArrNumSave[6]);

    // take the numbers near lucky numbers
    // getNearNumber = (NearNumberList *)malloc(sizeof(NearNumberList));
    p2  = (NearNumberList *)malloc(sizeof(NearNumberList));
    getNearNumber = NULL;
    for(i=1; i<34; i++) { // 1-33
        if (i == 1) {
            tempj = i+1; //
        } else if (i == 33) {
            tempi = i-1;
        } else {
            tempi = i-1;
            tempj = i+1;
        }
        for (j=1; j<7; j++) {
            if (i == ArrNumSave[j]) {
                if (i == 1) {
                    p1 = (NearNumberList *)malloc(sizeof(NearNumberList));
                    p1->num = tempi;
                    p1->next = NULL;
                    if (getNearNumber == NULL) {
                        getNearNumber = p1;
                    } else {
                        p2->next = p1;
                    }
                    p2 = p1;
                } else if (i == 33) {
                    p1 = (NearNumberList *)malloc(sizeof(NearNumberList));
                    p1->num = tempj;
                    p1->next = NULL;
                    if (getNearNumber == NULL) {
                        getNearNumber = p1;
                    } else {
                        p2->next = p1;
                    }
                    p2 = p1;
                } else {
                    p1 = (NearNumberList *)malloc(sizeof(NearNumberList));
                    p3 = (NearNumberList *)malloc(sizeof(NearNumberList));
                    p1->num = tempi;
                    p3->num = tempj;
                    p3->next = p1;
                    p1->next = NULL;
                    if (getNearNumber == NULL) {
                        getNearNumber = p3;
                    } else {
                        p2->next = p3;
                    }
                    p2 = p1;
                }
                break;
            }
        }
    }

    for(i=0; i<12; i++)
        NumberNearLastStageNumber.Num[i] = 0;

    i = 0;
    printf("Numbers near last stage are:\n");
    while(getNearNumber != NULL) {
        printf("%d,", getNearNumber->num);
        NumberNearLastStageNumber.Num[i++] = getNearNumber->num;
        getNearNumber = getNearNumber->next;
    }
    printf("\n");
    fclose(FileP);
    return i;
}

/**
 * static int NumberRangeCalculate: calculate range (Only for red balls)
 */
int NumberRangeCalculate(int ball1, int ball2, int ball3, int ball4, int ball5, int ball6)
{
    int range1, range2, range3, i;
    int *Redball;

    range1 = 0;
    range2 = 0;
    range3 = 0;

    Redball = (int *)malloc(6*sizeof(int));
    Redball[0] = ball1;
    Redball[1] = ball2;
    Redball[2] = ball3;
    Redball[3] = ball4;
    Redball[4] = ball5;
    Redball[5] = ball6;

    for(i=0; i<6; i++) {
        if ((Redball[i]>0) && (Redball[i]<12)) {
            // 1-11
            range1++;
        } else if ((Redball[i]>11) && (Redball[i]<23)) {
            // 12-22
            range2++;
        } else if ((Redball[i]>22) && (Redball[i]<34)) {
            // 23-33
            range3++;
        }
    }

    if((range1==2) && (range2==2) && (range3==2)){
        // 222
        return TWOTWOTWO;
    } else if ((range1==1) && (range2==2) && (range3==3)) {
        // 123
        return ONETWOTHREE;
    } else if ((range1==1) && (range2==3) && (range3==2)) {
        // 132
        return ONETHREETWO;
    } else if ((range1==3) && (range2==1) && (range3==2)) {
        // 312
        return THREEONETWO;
    } else if ((range1==3) && (range2==2) && (range3==1)) {
        // 321
        return THREETWOONE;
    } else if ((range1==2) && (range2==1) && (range3==3)) {
        // 213
        return TWOONETHREE;
    } else if ((range1==2) && (range2==1) && (range3==1)) {
        // 231
        return TWOTHREEONE;
    } else if ((range1==4) && (range2==1) && (range3==1)) {
        // 411
        return FOURONEONE;
    } else if ((range1==1) && (range2==4) && (range3==1)) {
        // 141
        return ONEFOURONE;
    } else if ((range1==1) && (range2==1) && (range3==4)) {
        // 114
        return ONEONEFOUR;
    } else if ((range1==3) && (range2==3) && (range3==0)) {
        // 330
        return THREETHREEO;
    } else if ((range1==3) && (range2==0) && (range3==3)) {
        // 303
        return THREEOTHREE;
    } else if ((range1==0) && (range2==3) && (range3==3)) {
        // 033
        return OTHREETHREE;
    } else if ((range1==0) && (range2==4) && (range3==2)) {
        // 042
        return OFOURTWO;
    } else if ((range1==2) && (range2==4) && (range3==0)) {
        // 240
        return TWOFOURO;
    } else if ((range1==4) && (range2==2) && (range3==0)) {
        // 420
        return FOURTWOO;
    } else if ((range1==4) && (range2==0) && (range3==2)) {
        // 402
        return FOUROTWO;
    } else if ((range1==2) && (range2==0) && (range3==4)) {
        // 204
        return TWOOFOUR;
    } else if ((range1==0) && (range2==2) && (range3==4)) {
        // 024
        return OTWOFOUR;
    } else if ((range1==5) && (range2==0) && (range3==1)) {
        // 501
        return FIVEOONE;
    } else if ((range1==1) && (range2==0) && (range3==5)) {
        // 105
        return ONEOFIVE;
    } else if ((range1==1) && (range2==5) && (range3==0)) {
        // 150
        return ONEFIVEO;
    } else if ((range1==0) && (range2==1) && (range3==5)) {
        // 015
        return OONEFIVE;
    } else if ((range1==5) && (range2==1) && (range3==0)) {
        // 510
        return FIVEONEO;
    } else if ((range1==1) && (range2==5) && (range3==1)) {
        // 051
        return OFIVEONE;
    } else if ((range1==0) && (range2==0) && (range3==6)) {
        // 006
        return OOSIX;
    } else if ((range1==6) && (range2==0) && (range3==0)) {
        // 600
        return SIXOO;
    } else if ((range1==0) && (range2==6) && (range3==0)) {
        // 060
        return OSIXO;
    }
}

/**
 * static int* GetAllStages(): Get all stages in LuckyNumber.txt
 * and return the pointer of the array
 */
static int* GetAllStages()
{
    FILE *AllStageLuckyNumberP;
    int *ArrSaveStages;
    int i, SaveStageN, SaveStageI = 0;

    printf("\nReading all lucky numbers...\n");
    if ((AllStageLuckyNumberP = OpenAFile("LuckyNumbers.txt", "a+")) == NULL) {
        exit(1);
    }
    // if \n is found means there is one stage.
    SaveStageN = 0;
    while(!feof(AllStageLuckyNumberP))
    {
        if (fgetc(AllStageLuckyNumberP) == '\n')
            SaveStageN++;
    }
    printf("SaveStageN is :%d\n", SaveStageN);
    SaveStageI = SaveStageN;
    if (SaveStageN != 0) {
        SaveStageI--;
        printf("SaveStageI is :%d\n", SaveStageI);
        // set the file point to head.
        if (fseek(AllStageLuckyNumberP, 0L, SEEK_SET) == -1) {
            printf("Set file point to head failed.\n");
            fclose(AllStageLuckyNumberP);
            exit(1);
        }
        GlobalArrSaveStage = malloc(SaveStageN*sizeof(int));
        i = 0;
        fscanf(AllStageLuckyNumberP, "%d", &GlobalArrSaveStage[i++]);
        //printf("Get stage:%d\n", GlobalArrSaveStage[i-1]);
        do {
            if (fgetc(AllStageLuckyNumberP) == '\n') {
                fscanf(AllStageLuckyNumberP, "%d", &GlobalArrSaveStage[i++]);
                //printf("Get stage:%d\n", GlobalArrSaveStage[i-1]);
            }
        } while(!feof(AllStageLuckyNumberP));
        // close the file.
        fclose(AllStageLuckyNumberP);
        return GlobalArrSaveStage;
    } else {
        printf("No stage no lucky number yet!\n");
        printf("*****Close file %s.*****\n", "LuckyNumbers.txt");
        fclose(AllStageLuckyNumberP);
        return NULL;
    }
}

/**
 * UpdateDatabaseAutomatically() update database automatically
 */
int UpdateDatabaseAutomatically()
{
    int ret, i, JudgeStageExistN;
    FILE *AutoUpdateFileP, *AllStageLuckyNumberP;
    int AutoUpdateBuf[8]; // save one stage infomation
    int *ArrSaveStages, SaveStageN, SaveStageI = 0;

    printf("\n\n\n\nYou've choosed to update the Caipiao database automatically.\n");
    printf("***********************************************************************************\n");
    printf("ATTENTION!!! WHILE YOU CHOOSE THE OPTION, YOU NEED TO KNWO THAT THE FORMAT OF THE\n");
    printf("FILE WHICH IS GOING TO INSERT SHOULD BE ORDERED FROM LASTEST STAGE TO OLDEST!!!!!\n");
    printf("OR IT WILL BE UNBELIEVABLE THE NEXT CALCULATE OPERATIONS!!!!\n");
    printf("***********************************************************************************\n");

    printf("\nReading all lucky numbers...\n");
    if ((AllStageLuckyNumberP = OpenAFile("LuckyNumbers.txt", "a+")) == NULL) {
        exit(1);
    }
    // if \n is found means there is one stage.
    SaveStageN = 0;
    while(!feof(AllStageLuckyNumberP))
    {
        if (fgetc(AllStageLuckyNumberP) == '\n')
            SaveStageN++;
    }
    //printf("SaveStageN is :%d\n", SaveStageN);
    SaveStageI = SaveStageN;
    if (SaveStageN != 0) {
        //SaveStageI--;
        //printf("SaveStageI is :%d\n", SaveStageI);
        // set the file point to head.
        if (fseek(AllStageLuckyNumberP, 0L, SEEK_SET) == -1) {
            printf("Set file point to head failed.\n");
            fclose(AutoUpdateFileP);
            exit(1);
        }
        ArrSaveStages = malloc(SaveStageN*sizeof(int));
        i = 0; // i is for ArrSaveStages array.
        fscanf(AllStageLuckyNumberP, "%d", &ArrSaveStages[i++]);
        //printf("%d:Get current Stage:%d\n", i, ArrSaveStages[i-1]);
        do {
            if (fgetc(AllStageLuckyNumberP) == '\n') {
                fscanf(AllStageLuckyNumberP, "%d", &ArrSaveStages[i++]);
            }
        } while(!feof(AllStageLuckyNumberP));
    } else {
        printf("No stage no lucky number yet!\n");
    }

        if ((AutoUpdateFileP=fopen("Caipiao.txt", "rw+")) == NULL) {
            printf("File open failed.\n");
            exit(1);
        }
        while(!feof(AutoUpdateFileP))
        {
            JudgeStageExistN = 0;
            if (!fscanf(AutoUpdateFileP, "%d%d%d%d%d%d%d%d", AutoUpdateBuf,AutoUpdateBuf+1,
                        AutoUpdateBuf+2,AutoUpdateBuf+3,AutoUpdateBuf+4,AutoUpdateBuf+5,AutoUpdateBuf+6,AutoUpdateBuf+7)) {
                printf("Read file error.\n");
                fclose(AutoUpdateFileP);
                exit(1);
            }
            for(i=0; i<SaveStageI; i++) {
                if (AutoUpdateBuf[0] == ArrSaveStages[i]) {
                    //printf("Current inserting stage(%d) is already in the database, pass.\n", AutoUpdateBuf[0]);
                    JudgeStageExistN = 1;
                    break;
                }
            }
            if (JudgeStageExistN != 1) {
                if (!fprintf(AllStageLuckyNumberP, "%6d,%02d,%02d,%02d,%02d,%02d,%02d,%02d\n", AutoUpdateBuf[0], AutoUpdateBuf[1],
                                                        AutoUpdateBuf[2], AutoUpdateBuf[3], AutoUpdateBuf[4],
                                                        AutoUpdateBuf[5], AutoUpdateBuf[6], AutoUpdateBuf[7])) {
                    printf("Write to Luckynumber.txt file error.\n");
                    fclose(AllStageLuckyNumberP);
                    exit(1);
                }
                printf("Stage %d is instered successfully.\n", AutoUpdateBuf[0]);
            }
            fgetc(AutoUpdateFileP); // read \n character
        }
        printf("Update Over...\n");
    fclose(AutoUpdateFileP);
    fclose(AllStageLuckyNumberP);
}

/**
 * UpdateDatabaseHandly() update database by your hand
 */
int UpdateDatabaseHandly()
{
    U8 ArrNum[8];
    int i, *ArrAllStage, AllStageN;
    char c;
    FILE *FileToWriteP;

    printf("\nYou've choosed to insert one stage handly.\n");
    printf("Enter stage(2013001-9999999), redBall(1-33), blueBall(1-16):\n");
    while(1) {
        for(i=0; i<8; i++) {
            while(1) {
                if (i == 0) {
                    printf("stage: ");
                } else if (i == 7) {
                    printf("blue ball: ");
                } else {
                    printf("red ball%d: ", i);
                }
                scanf("%d", &ArrNum[i]);
                if ((i==7) && ((ArrNum[i]>17) || (ArrNum[i]<1))) {
                    printf("You've entered invailed blue(1-16) number.Reinput.\n");
                } else if(((i==1)||(i==2)||(i==3)||(i==4)||(i==5)||(i==6)) && ((ArrNum[i]>33) || (ArrNum[i]<1))) {
                    printf("You've entered invailed red(1-33) number.Reinput.\n");
                } else if ((((ArrNum[i])>9999999) || (ArrNum[i]<2013001)) && (i==0)) {
                    printf("You've entered invailed stage number.Reinput.\n");
                } else {
                    break;
                }
            }
        }
        printf("Your number is:\n");
        printf("stage\tred1\tred2\tred3\tred4\tred5\tred6\tblue\n");
        printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",ArrNum[0], ArrNum[1], ArrNum[2], ArrNum[3], ArrNum[4], ArrNum[5], ArrNum[6], ArrNum[7]);
        printf("Sure to input(Y/N)?");
        fflush(stdin);
        scanf("%c", &c);
        if((c=='y') || (c=='Y'))
            break;
        else if((c=='n') || (c=='N'))
            return 0;
    }
    printf("Checking whether the stage is exist or not.");
    // get all stage from LuckyNumber.txt
    if ((ArrAllStage=GetAllStages()) == NULL) {
        //
    } else {
        // calculate how many stages.
        AllStageN = _msize(GlobalArrSaveStage)/4;
        printf("Currently there are %d stages in the database.\n", AllStageN);
        for(i=0; i<AllStageN; i++) {
            if (GlobalArrSaveStage[i] == ArrNum[0]) {
                printf("Current inserting stage(%d) is already in the database, pass.\n", ArrNum[0]);
                return 0;
            }
        }
    }
    if ((FileToWriteP = OpenAFile("LuckyNumbers.txt", "a+")) == NULL) {
        exit(1);
    }
    if (!WriteToFile(FileToWriteP, ArrNum)) {
        exit(1);
    }
    printf("Stage %d is instered successfully.\n", ArrNum[0]);
    fclose(FileToWriteP);
    return 1;
}

/**
 * CalculateBestGroups() Calculate the best few groups from 17 million groups.
 */
int CalculateBestGroups()
{
    // hot & cold
    int FilterHotNumN, FilterColdNumN, allgroupNumber;
    // near last stage
    int FilterNearLastStageN, LastStageNearNumberN, *ArrFilterNearLastStageNumber;
    // courage & tow yard
    int FilterCourageYardGroupN, *ArrFilterCourageYardGourpNumberN, *ArrCourageYardNumInLuckyNumber , **ArrCourageYardPerGroup, *ArrAllGroupFitCourageN;
    // range
    int rangesN, *arrRanges, rangeRet;
    // interval
    int intervalN, *arrInterval, intervalRet;
    // AC value
    int ACValueN, *arrACValue, ACValueRet;
    // parity ratio
    int parityRatioN, *arrParityRatio, ParityRatioRet;
    // Biggest same number of mantissa
    int MantissaSameNumberN, *arrMantissaSameNumber, MantissaSameNumberRet;
    // the sum
    int sumChooseN, *arrSumValuechoose, sumValueRet;
    // consecutive number
    int consecutiveNumberChooseN, *arrConsecutiveNumber, consecutiveNumberRet;
    // re no.
    int reNochooseN, *arrReNo, reNoRet;
    GroupList listPre;
    // size ratio.
    int sizeRatioN, *arrSizeRatio, sizeRatioRet;
    // Maxinum Re-Number
    int maxinumReNumberN, *arrMaxinumReNumber, MaxinumReNumberRet;

    int i, j, a, b, tempi, tempj, showProcessI, showProcessJ;
    GroupList *p1, *p2, *DestroyP, *tempP;
    NearNumberList *DestroyNearP, *getNearLastStageNumber;
    FILE *fileP; // test need delete

    system("cls");
    printf("\n");
    printf("************************************************************************\n");
    printf(" In this step you are going to calculate and filter many groups. Please\n");
    printf(" do the down operations step by step. The operations will contain the\n");
    printf(" following steps:\n");
    printf(" 1. Generate and init groups.\n");
    printf(" 2. Filter hot and cold numbers.\n");
    printf(" 3. ADD LATER.\n");
    printf("************************************************************************\n");

    printf("\n\n<---------------------------Step One. Generate groups------------------------->\n");
    // generate all gourps.
    allgroupNumber = GenerateAllGroup(33, 16);
    printf("<----------------------------------Generate Over------------------------------>\n");

    /**
    * Next filter hot & cold num..
    */
    printf("\n\n<-----------------------Step Two. Filter hot & cold num.--------------------->\n");
    // Hot ball filter.
    printf("Calculate Hot & Cold Numbers...\n");
    GenerateHotColdNumber();
    printf("Enter how many hot and cold number you want to filter(0-8, 0 is not to filter.):\n");
    while(1) {
        printf("Hot:");
        scanf("%d", &FilterHotNumN);
        if((FilterHotNumN > 8) || (FilterHotNumN < 0)) {
            printf("You entered invaild number. Input again(1-8).\n");
        } else {
            break;
        }
    }
    while(1) {
        printf("Cold:");
        scanf("%d", &FilterColdNumN);
        if((FilterColdNumN > 8) || (FilterColdNumN < 0)) {
            printf("You entered invaild number. Input again(1-8, 0 is not to filter.).\n");
        } else {
            break;
        }
    }

    printf("Filtering...\n");
    // init the new list;
    GroupListAfterFilterHotNumber = NULL; // init a NULL list
    p2 = (GroupList *)malloc(sizeof(GroupList));
    printf("Filtering Hot number.\n");
    //printf("allgroupNumber : %d.\n", allgroupNumber);
    showProcessI = 0; // for showing process bar.
    showProcessJ = 0; // for calculate numbers after filtering hot number.
    for(i=0; i<allgroupNumber; i++) {
        // each group
        tempi = 0;
        showProcessI++;
        showProcessBar(showProcessI, allgroupNumber);
        for(j=0; j<6; j++) {
            // each number
            for(a=0; a<8; a++) {
                // HotNumber list
                //printf("ArrAllGroup[%d][%d]:%d, HotNumber[%d]:%d\n", i, j, ArrAllGroup[i][j], a, HotNumber[a]);
                if (ArrAllGroup[i][j] == HotNumber[a]) {
                    tempi++;
                    break;
                }
            }
        }
        if ((tempi < (FilterHotNumN+1)) && (tempi>0)) {
            // Equal number is small than you set.
            showProcessJ++;
            p1 = (GroupList *)malloc(sizeof(GroupList));
            for(b=0; b<7; b++) { // modified by zjz 20131222 add blue ball element
                p1->Ball[b] = ArrAllGroup[i][b];
            }
            p1->next = NULL;
            if (GroupListAfterFilterHotNumber == NULL) {
                GroupListAfterFilterHotNumber = p1;
            } else {
                p2->next = p1;
            }
            p2 = p1;
        }
    }
    printf("Hot number filter successfully. Going to filter cold number.\n");
    GroupListAfterFilterColdNumber = NULL;
    p2 = (GroupList *)malloc(sizeof(GroupList));
    // DestroyP: save the list head after filter hot number to destroy the list
    DestroyP = (GroupList *)malloc(sizeof(GroupList));
    DestroyP = GroupListAfterFilterHotNumber;
    tempj = 0;
    showProcessI = 0; // for showing process bar.
    if (GroupListAfterFilterHotNumber == NULL) {
        printf("None Number was filtered last step. Please check!\n");
        exit(1);
    } else {
        while(GroupListAfterFilterHotNumber != NULL) {
            tempi = 0;
            showProcessI++;
            showProcessBar(showProcessI, showProcessJ);
            // six red numbers
            for(j=0; j<6; j++) {
                // each number
                for(a=0; a<8; a++) {
                    // eight times to compare with cold numbers.
                    if (GroupListAfterFilterHotNumber->Ball[j] == ColdNumber[a]) {
                        tempi++;
                        break;
                    }
                }
            }
            if((tempi>0) && (tempi<(FilterColdNumN+1))) {
                tempj++;
                p1 = (GroupList *)malloc(sizeof(GroupList));
                for(b=0; b<7; b++) {
                    p1->Ball[b] = GroupListAfterFilterHotNumber->Ball[b];
                }
                p1->next = NULL;
                if (GroupListAfterFilterColdNumber == NULL) {
                    // head
                    GroupListAfterFilterColdNumber = p1;
                } else {
                    p2->next = p1;
                }
                p2 = p1;
            }
            GroupListAfterFilterHotNumber = GroupListAfterFilterHotNumber->next;
        }
    }
    showProcessJ = tempj;
    printf("Delete the cache...\n");
    printf("Free hot number array.\n");
    free(HotNumber);
    printf("Free cold number array.\n");
    free(ColdNumber);
    printf("Free all groups array.\n");
    //for(i=0; i<allgroupNumber; i++) {
        //free(ArrAllGroup++);
    //}
    printf("Delete list after filter hot number.\n");
    if (DestroyP == NULL) {
        printf("There mayby a error set DestroyP value before, please check.\n");
        exit(1);
    } else {
        while(DestroyP->next != NULL) {
            p1 = DestroyP->next;
            free(DestroyP);
            DestroyP = p1;
        }
    }
    GroupListAfterFilterHotNumber = NULL;
    printf("Cache clear over!\n");
    printf("Filter hot and cold number over.Now there are still %d groups left.\n", tempj);
    printf("<------------------------Filter hot and cold numbers over------------------------->\n");

    /**
    * Next filter the number by last stage number.
    */
    printf("\n\n<--------------------------Step Three. Filter near numbers-------------------------->\n");
    printf("Generate near numbers by last stage's lucky number.\n");
    // get how many stage numbers.
    LastStageNearNumberN = FilterLastStageNumber();

    printf("Enter how many numbers to filter altogether(0-6 supported.):");
    while(1) {
        scanf("%d", &FilterNearLastStageN);
        if ((FilterNearLastStageN <0) || (FilterNearLastStageN>6)) {
            printf("You entered invaild number. Enter again.(0-6)\n");
        } else {
            printf("Then enter how many numbers you want to include each:\n");
            ArrFilterNearLastStageNumber = (int *)malloc(FilterNearLastStageN*sizeof(int));
            for(i=0; i<FilterNearLastStageN; i++) {
                while(1) {
                    printf("%d:", i+1);
                    scanf("%d", &ArrFilterNearLastStageNumber[i]);
                    if ((ArrFilterNearLastStageNumber[i] < 0) && (ArrFilterNearLastStageNumber[i] > 6)) {
                        printf("You entered invalid number(0-6). Enter again.\n");
                    } else {
                        break;
                    }
                }
            }
            break;
        }
    }
    printf("Your numbers are:");
    for(i=0; i<FilterNearLastStageN; i++) {
        printf("%d ", ArrFilterNearLastStageNumber[i]);

    }
    printf("\n");

    printf("Filtering...\n");
    GroupListAfterFilterNearLastStage = (GroupList *)malloc(sizeof(GroupList));
    GroupListAfterFilterNearLastStage = NULL;
    // DestroyP: save the list head after filter near last stage lucky number to destroy the list
    DestroyP = GroupListAfterFilterColdNumber;
    p2 = (GroupList *)malloc(sizeof(GroupList));
    tempj = 0; // calculate all groups after filter near last stage number.
    showProcessI = 0;
    while(GroupListAfterFilterColdNumber != NULL) {
        //every group after filter cold number.
        tempi = 0;
        showProcessI++;
        showProcessBar(showProcessI, showProcessJ);
        for(i=0; i<6; i++) {
            // each red ball
            //printf("current ball number:%d\n", GroupListAfterFilterColdNumber->Ball[i]);
            for(j=0; j<LastStageNearNumberN; j++) {
                if (NumberNearLastStageNumber.Num[j] == GroupListAfterFilterColdNumber->Ball[i]) {
                    tempi++;
                    break;
                }
            }
        }
        //printf("tempi:%d\n", tempi);
        for(i=0; i<FilterNearLastStageN; i++) {
            if(tempi == ArrFilterNearLastStageNumber[i]) {
                tempj++;
                p1 = (GroupList *)malloc(sizeof(GroupList));
                for(j=0; j<7; j++) {
                    p1->Ball[j] = GroupListAfterFilterColdNumber->Ball[j];
                }
                p1->next = NULL;
                if(GroupListAfterFilterNearLastStage == NULL) {
                    GroupListAfterFilterNearLastStage = p1;
                } else {
                    p2->next = p1;
                }
                p2 = p1;
                break;
            }
        }
        GroupListAfterFilterColdNumber = GroupListAfterFilterColdNumber->next;
    }
    showProcessJ = tempj;
    showProcessI = 0;
    printf("Clear list cache after filter cold number...\n");
    if (DestroyP == NULL) {
        printf("There mayby a error set DestroyP value before, please check.\n");
        exit(1);
    } else {
        while(DestroyP->next != NULL) {
            p1 = DestroyP->next;
            free(DestroyP);
            DestroyP = p1;
        }
    }
    GroupListAfterFilterColdNumber = NULL;

    printf("Clear cache over.\n");
    printf("Filter near last stage lucky number over. There are still %d gourps left.\n", tempj);
    printf("<-------------------------------Filter near numbers over---------------------------->\n");

    /**
    * Next filter Tow&courage yard.
    */
    printf("\n\n<-------------------------------Filter Tow&courage yard---------------------------->\n\n");
    printf("#################################### INFORMATIONS #################################\n");
    printf("#     Courage yard and tow yard filtration is to filter by one or two or more n-  #\n");
    printf("#  -umber gourps you have setted. You can set five groups numbers, and each group #\n");
    printf("#  is AND relationship. And each gourp you can set how many numbers it should be  #\n");
    printf("#  contained. That's all, thank you ^_^                                           #\n");
    printf("#################################### INFORMATIONS #################################\n\n");
    printf("First: Enter how many groups, you want to AND:\n");
    scanf("%d", &FilterCourageYardGroupN);
    ArrFilterCourageYardGourpNumberN = (int *)malloc(sizeof(int)*FilterCourageYardGroupN);
    printf("    OK. You have choosed %d gourps to filter. Now enter how many numbers for each\n", FilterCourageYardGroupN);
    printf("group. You will need to input one group by one.\n");
    for (i=0; i<FilterCourageYardGroupN; i++) {
        printf("group%d:", i+1);
        scanf("%d", &ArrFilterCourageYardGourpNumberN[i]);
    }
    printf("    Now input each group's numbers--the courage yard. You will have to enter the\n");
    printf("same number as you have setted last step.");

    // malloc a FilterCourageYardGroupN groups 33 numbers group
    ArrCourageYardPerGroup = (int *)malloc(FilterCourageYardGroupN*sizeof(int *));
    for(i=0; i<FilterCourageYardGroupN; i++)
        ArrCourageYardPerGroup[i] = (int)malloc(33*sizeof(int));

    // init the **arr;
    for(i=0; i<FilterCourageYardGroupN; i++) {
        for(j=0; j<ArrFilterCourageYardGourpNumberN[i]; j++) {
            ArrCourageYardPerGroup[i][j] = 0;
        }
    }

    for(i=0; i<FilterCourageYardGroupN; i++) {
        printf("Group%d. You have %d numbers.\n", i+1, ArrFilterCourageYardGourpNumberN[i]);
        for(j=0; j<ArrFilterCourageYardGourpNumberN[i]; j++) {
            printf("The %d Number:", j+1);
            scanf("%d", &ArrCourageYardPerGroup[i][j]);
        }
    }
    printf("Now enter how many numbers you want to INCLUDE in the LUCKY numbers.\n");
    ArrCourageYardNumInLuckyNumber = (int)malloc(FilterCourageYardGroupN*sizeof(int));

    // init 0
    for(i=0; i<FilterCourageYardGroupN; i++) {
        ArrCourageYardNumInLuckyNumber[i] = 0;
    }
    for(i=0; i<FilterCourageYardGroupN; i++) {
        printf("Group%d:", i+1);
        scanf("%d", &ArrCourageYardNumInLuckyNumber[i]);
    }
    printf("Your courage numbers are:\n");
    for(i=0; i<FilterCourageYardGroupN; i++) {
        printf("Group %d: ", i+1);
        for(j=0; j<ArrFilterCourageYardGourpNumberN[i]; j++) {
            printf("%d  ", ArrCourageYardPerGroup[i][j]);
        }
        printf("And you will choose %d numbers from these numbers.", ArrCourageYardNumInLuckyNumber[i]);
        printf("\n");
    }
    // this malloc is for saving the numbers which all_groups fit the courage number group.
    ArrAllGroupFitCourageN = (int *)malloc(sizeof(int)*FilterCourageYardGroupN);
    // init
    for(i=0; i<FilterCourageYardGroupN; i++)
        ArrAllGroupFitCourageN[i] = 0;

    printf("Filtering...\n");

    // init new list
    GroupListAfterFilterCourageYard = NULL;
    p2 = (GroupList *)malloc(sizeof(GroupList));
    // DestroyP is to delte the used list to free space.
    DestroyP = GroupListAfterFilterNearLastStage;
    tempj = 0; // counting how many groups are fit.

    if(GroupListAfterFilterNearLastStage == NULL) {
        printf("GroupListAfterFilterNearLastStage is NULL\n");
        exit(1);
    }
    while(GroupListAfterFilterNearLastStage != NULL) {
        // Traversal all the groups after filtering from last stage near numbers.
        showProcessI++;
        showProcessBar(showProcessI, showProcessJ);
        for(i=0; i<FilterCourageYardGroupN; i++) {
            //No.i+1 gourp
            //printf("Group%d:", i+1);
            ArrAllGroupFitCourageN[i] = 0; // for calculate how many numbers one of all group's numbers are fit
            for(j=0; j<ArrFilterCourageYardGourpNumberN[i]; j++) {
                // each number of each group
                //printf("\nCourage number:%d ball:", ArrCourageYardPerGroup[i][j]);
                for(a=0; a<7; a++) {
                    // Traversal all balls(include red ball and blue ball).
                    //printf("%d ", GroupListAfterFilterNearLastStage->Ball[a]);
                    if(ArrCourageYardPerGroup[i][j] == GroupListAfterFilterNearLastStage->Ball[a]) {
                        //Find the courage yard
                        ArrAllGroupFitCourageN[i]++;
                        break;
                    }
                }
            }
        }
        //printf(".");
        //system("pause");
        tempi = 0;
        for (i=0; i<FilterCourageYardGroupN; i++) {
            if (ArrAllGroupFitCourageN[i] >= ArrCourageYardNumInLuckyNumber[i]) {
                tempi++;
            }
        }
        if ( tempi==FilterCourageYardGroupN ) {
            tempj++; // counting how many groups are fit.
            p1 = (GroupList *)malloc(sizeof(GroupList));
            for(a=0; a<7; a++) {
                p1->Ball[a] = GroupListAfterFilterNearLastStage->Ball[a];
            }
            p1->next = NULL;
            if(GroupListAfterFilterCourageYard == NULL) {
                GroupListAfterFilterCourageYard = p1;
            } else {
                p2->next = p1;
            }
            p2 = p1;
        }
        GroupListAfterFilterNearLastStage = GroupListAfterFilterNearLastStage->next;
    }
    showProcessJ = tempj;
    showProcessI = 0;
    printf("Filter over. Delete caches....\n");
    if (DestroyP == NULL) {
        printf("There mayby a error set DestroyP value before, please check.\n");
        exit(1);
    } else {
        while(DestroyP->next != NULL) {
            p1 = DestroyP->next;
            free(DestroyP);
            DestroyP = p1;
        }
    }
    GroupListAfterFilterNearLastStage = NULL;
    printf("Filter courage yard over. there are still %d gourps left.\n", tempj);
    printf("<-------------------------------Filter Tow&courage yard Over------------------------->\n");

    /**
    * Next filter Number range.
    */
    printf("\n\n<-----------------------------------Filter Number Range------------------------------>\n\n");
    printf("#################################### INFORMATIONS #################################\n");
    printf("#    The step is to filter the groups by every group's number's range, such as: 22#\n");
    printf("# 2, 123, 312 etc. The range is designed by number range from 1-11, 12-22, 23-33 .#\n");
    printf("# You can choose one or more ranges. BUT no more than 28! You will have a choice  #\n");
    printf("# in my programme. That's all, thank you. ^ ^                                     #\n");
    printf("#################################### INFORMATIONS #################################\n\n");

    // show a table.
    printf("All stage's table is here:\n");
    showAllStageRange();
    printf("Now input how many ranges you want include(1-28):");
    while(1) {
        scanf("%d", &rangesN);
        if((rangesN>28) || (rangesN<0)) {
            printf("You have entered invalid number.Enter again(1-28):");
        } else {
            break;
        }
    }
    arrRanges = (int *)malloc(rangesN*sizeof(int));
    printf("Then input your kind of ranges, the relationship of one number to a range is:\n");
    printf("0 :222; 1 :123; 2 :132; 3 :312; 4 :321; 5 :213; 6 :231; 7 :411; 8 :141; 9 :114; 10:330;\n");
    printf("11:303; 12:033; 13:042; 14:240; 15:420; 16:402; 17:204; 18:024; 19:501; 20:105; 21:150;\n");
    printf("22:015; 23:510; 24:051; 25:006; 26:600; 27:060;");
    printf("Now input, you have choosed %d ranges.\n", rangesN);
    for(i=0; i<rangesN; i++) {
        while(1){
            printf("range%d:",i+1);
            scanf("%d", &arrRanges[i]);
            if((arrRanges[i]>28) || (arrRanges[i]<0)) {
                printf("You have entered invalid number. Enter again(1-28):");
            } else {
                break;
            }
        }
    }
    for(i=0; i<rangesN; i++) {
        if(i==0)
            printf("Your ranges are: ");
        printf("%d ", arrRanges[i]);
    }
    printf("\n");
    printf("Filtering...\n");

    // new list for save groups.
    GroupListAfterFilterRanges = NULL;
    p2 = (GroupList *)malloc(sizeof(GroupList));
    // Delete the cache. you know that.
    DestroyP = GroupListAfterFilterCourageYard;
    tempj = 0;
    while(GroupListAfterFilterCourageYard != NULL) {
        rangeRet = getRange(GroupListAfterFilterCourageYard);
        showProcessI++;
        showProcessBar(showProcessI, showProcessJ);
        for(i=0; i<rangesN; i++) {
            if(rangeRet == arrRanges[i]) {
                tempj++;
                p1 = (GroupList *)malloc(sizeof(GroupList));
                for(j=0; j<7; j++) {
                    p1->Ball[j] = GroupListAfterFilterCourageYard->Ball[j];
                }
                p1->next = NULL;
                if(GroupListAfterFilterRanges == NULL) {
                    GroupListAfterFilterRanges = p1;
                } else {
                    p2->next = p1;
                }
                p2 = p1;
                break;
            }
        }
        GroupListAfterFilterCourageYard = GroupListAfterFilterCourageYard->next;
    }
    showProcessJ = tempj;
    showProcessI = 0;
    printf("Delete Caches.\n");
    if (DestroyP == NULL) {
        printf("There mayby a error set DestroyP value before, please check.\n");
        exit(1);
    } else {
        while(DestroyP->next != NULL) {
            p1 = DestroyP->next;
            free(DestroyP);
            DestroyP = p1;
        }
    }
    GroupListAfterFilterCourageYard = NULL;
    printf("Filter ranges over. There are still %d groups left.\n", tempj);
    printf("<---------------------------------Filter Number Range Over--------------------------->\n");

    /**
     * Filter Interval
     */
    printf("\n\n<---------------------------------------Filter Interval-------------------------------->\n");
    printf("#################################### INFORMATIONS #################################\n");
    printf("#    This step is to filter the groups by interval between two near numbers. my s-#\n");
    printf("# oft will judge this only in red balls, not include blue ball. In this step, you #\n");
    printf("# can see intervals of stages before. And you also can choose interval number for #\n");
    printf("# yourself. Also, 1, set interval number; 2, set every interval number.   ^ ^     #\n");
    printf("#################################### INFORMATIONS #################################\n\n");

    printf("Interval of stages before is as follows:\n");
    // show a table.
    showIntervalStagesbefore();
    printf("Now input the number you want to filter ALTOGETHER(1-28):");
    while(1) {
        scanf("%d", &intervalN);
        if((intervalN>28) || (intervalN<0)) {
            printf("You have entered invalid number.Enter again(1-28):");
        } else {
            break;
        }
    }
    arrInterval = (int *)malloc(intervalN*sizeof(int));
    printf("Now input, you have choosed %d Interval numbers.\n", intervalN);
    for(i=0; i<intervalN; i++) {
        while(1){
            printf("Interval%d:",i+1);
            scanf("%d", &arrInterval[i]);
            if((arrInterval[i]>28) || (arrInterval[i]<0)) {
                printf("You have entered invalid number. Enter again(1-28):");
            } else {
                break;
            }
        }
    }
    for(i=0; i<intervalN; i++) {
        if(i==0)
            printf("Your Interval numbers are: ");
        printf("%d ", arrInterval[i]);
    }
    printf("\n");
    printf("Filtering...\n");

    // new list for save groups.
    GroupListAfterFilterIntervalNumber = NULL;
    p2 = (GroupList *)malloc(sizeof(GroupList));
    // Delete the cache. you know that.
    DestroyP = GroupListAfterFilterRanges;
    tempj = 0;
    while(GroupListAfterFilterRanges != NULL) {
        intervalRet = getInterval(GroupListAfterFilterRanges);
        showProcessI++;
        showProcessBar(showProcessI, showProcessJ);
        for(i=0; i<intervalN; i++) {
            if(intervalRet == arrInterval[i]) {
                tempj++;
                p1 = (GroupList *)malloc(sizeof(GroupList));
                for(j=0; j<7; j++) {
                    p1->Ball[j] = GroupListAfterFilterRanges->Ball[j];
                }
                p1->next = NULL;
                if(GroupListAfterFilterIntervalNumber == NULL) {
                    GroupListAfterFilterIntervalNumber = p1;
                } else {
                    p2->next = p1;
                }
                p2 = p1;
                break;
            }
        }
        GroupListAfterFilterRanges = GroupListAfterFilterRanges->next;
    }
    showProcessJ = tempj;
    showProcessI = 0;
    printf("Delete Caches.\n");
    if (DestroyP == NULL) {
        printf("There mayby a error set DestroyP value before, please check.\n");
        exit(1);
    } else {
        while(DestroyP->next != NULL) {
            p1 = DestroyP->next;
            free(DestroyP);
            DestroyP = p1;
        }
    }
    GroupListAfterFilterRanges = NULL;
    printf("Filter Interval numbers over. There are still %d groups left.\n", tempj);
    printf("<------------------------------------Filter Interval Over------------------------------>\n");


    /**
     * AC value filter.
     */
    printf("\n\n<---------------------------------------Filter AC Value-------------------------------->\n");
    printf("#################################### INFORMATIONS ##################################\n");
    printf("#     This step, AV value filter step. AC value is a function to caculate discrete #\n");
    printf("# of a number group. And in Shuangse ball games, AV value is from 0-10 without blu #\n");
    printf("# -e balls, and from 0-15 with blue balls. In my soft, I will not include the blue #\n");
    printf("# because few people do this. Now DO IT!!!. Two steps, as follows.  ^ ^            #\n");
    printf("#################################### INFORMATIONS ##################################\n\n");

    printf("AC values of stages before is as follows:\n");
    // show a table.
    showACValue();
    printf("Now input the number you want to filter ALTOGETHER(1-11):");
    while(1) {
        scanf("%d", &ACValueN);
        if((ACValueN>11) || (ACValueN<0)) {
            printf("You have entered invalid number.Enter again(1-28):");
        } else {
            break;
        }
    }
    arrACValue = (int *)malloc(ACValueN*sizeof(int));
    printf("Now input, you have choosed %d AC value numbers.\n", ACValueN);
    for(i=0; i<ACValueN; i++) {
        while(1){
            printf("AC value%d:",i+1);
            scanf("%d", &arrACValue[i]);
            if((arrACValue[i]>11) || (arrACValue[i]<0)) {
                printf("You have entered invalid number. Enter again(1-11):");
            } else {
                break;
            }
        }
    }
    for(i=0; i<ACValueN; i++) {
        if(i==0)
            printf("Your choosed AC values are: ");
        printf("%d ", arrACValue[i]);
    }
    printf("\n");
    printf("Filtering...\n");

    // new list for save groups.
    GroupListAfterFilterACValue = NULL;
    p2 = (GroupList *)malloc(sizeof(GroupList));
    // Delete the cache. you know that.
    DestroyP = GroupListAfterFilterIntervalNumber;
    tempj = 0;
    while(GroupListAfterFilterIntervalNumber != NULL) {
        ACValueRet = getACValue(GroupListAfterFilterIntervalNumber);
        showProcessI++;
        showProcessBar(showProcessI, showProcessJ);
        for(i=0; i<ACValueN; i++) {
            if(ACValueRet == arrACValue[i]) {
                tempj++;
                p1 = (GroupList *)malloc(sizeof(GroupList));
                for(j=0; j<7; j++) {
                    p1->Ball[j] = GroupListAfterFilterIntervalNumber->Ball[j];
                }
                p1->next = NULL;
                if(GroupListAfterFilterACValue == NULL) {
                    GroupListAfterFilterACValue = p1;
                } else {
                    p2->next = p1;
                }
                p2 = p1;
                break;
            }
        }
        GroupListAfterFilterIntervalNumber = GroupListAfterFilterIntervalNumber->next;
    }
    showProcessJ = tempj;
    showProcessI = 0;
    printf("Delete Caches.\n");
    if (DestroyP == NULL) {
        printf("There mayby a error set DestroyP value before, please check.\n");
        exit(1);
    } else {
        while(DestroyP->next != NULL) {
            p1 = DestroyP->next;
            free(DestroyP);
            DestroyP = p1;
        }
    }
    GroupListAfterFilterIntervalNumber = NULL;
    printf("Filter AC value over. There are still %d groups left.\n", tempj);
    printf("<------------------------------------Filter AC Value Over------------------------------>\n");

    /**
     * Parity Ratio filter.
     */
    printf("\n\n<------------------------------------Filter Parity Ratio------------------------------>\n");
    printf("#################################### INFORMATIONS ##################################\n");
    printf("#     This is step is Parity Ratio Filter. There are three options to choose: 3:3  #\n");
    printf("# 4:2 2:4 5:1 1:5 6:0 0:6. You can also choose one or more options to filter, But  #\n");
    printf("# no more than 7. This step is easy, just go and do it.      ^ ^                   #\n");
    printf("#################################### INFORMATIONS ##################################\n\n");

    printf("Parity ratio of stages before is as follows:\n");
    // show a table.
    showParityRatio();
    printf("Now input the number you want to filter ALTOGETHER(1-7):");
    while(1) {
        scanf("%d", &parityRatioN);
        if((parityRatioN>7) || (parityRatioN<0)) {
            printf("You have entered invalid number.Enter again(1-7):");
        } else {
            break;
        }
    }
    arrParityRatio = (int *)malloc(parityRatioN*sizeof(int));
    printf("Now input, you have choosed %d parity ratio numbers.\n", parityRatioN);
    printf("0:(3:3); 1:(4:2); 2:(2:4); 3:(5:1); 4:(1:5); 5:(6:0); 6:(0:6)\n");
    for(i=0; i<parityRatioN; i++) {
        while(1){
            printf("ParityRatio%d:",i+1);
            scanf("%d", &arrParityRatio[i]);
            if((arrParityRatio[i]>7) || (arrParityRatio[i]<0)) {
                printf("You have entered invalid number. Enter again(1-7):");
            } else {
                break;
            }
        }
    }
    for(i=0; i<parityRatioN; i++) {
        if(i==0)
            printf("Your choosed parity ratio numbers are:\n");
        printf("%-9d ", arrParityRatio[i]);
    }
    printf("\n");
    printf("Filtering...\n");

    // new list for save groups.
    GroupListAfterFilterParityRatio = NULL;
    p2 = (GroupList *)malloc(sizeof(GroupList));
    // Delete the cache. you know that.
    DestroyP = GroupListAfterFilterACValue;
    tempj = 0;
    while(GroupListAfterFilterACValue != NULL) {
        ParityRatioRet = getParityRatio(GroupListAfterFilterACValue);
        showProcessI++;
        showProcessBar(showProcessI, showProcessJ);
        for(i=0; i<parityRatioN; i++) {
            if(ParityRatioRet == arrParityRatio[i]) {
                tempj++;
                p1 = (GroupList *)malloc(sizeof(GroupList));
                for(j=0; j<7; j++) {
                    p1->Ball[j] = GroupListAfterFilterACValue->Ball[j];
                }
                p1->next = NULL;
                if(GroupListAfterFilterParityRatio == NULL) {
                    GroupListAfterFilterParityRatio = p1;
                } else {
                    p2->next = p1;
                }
                p2 = p1;
                break;
            }
        }
        GroupListAfterFilterACValue = GroupListAfterFilterACValue->next;
    }
    showProcessJ = tempj;
    showProcessI = 0;
    printf("Delete Caches.\n");
    if (DestroyP == NULL) {
        printf("There mayby a error set DestroyP value before, please check.\n");
        exit(1);
    } else {
        while(DestroyP->next != NULL) {
            p1 = DestroyP->next;
            free(DestroyP);
            DestroyP = p1;
        }
    }
    GroupListAfterFilterACValue = NULL;
    printf("Filter parity ratio over. There are still %d groups left.\n", tempj);
    printf("<----------------------------------Filter Parity Ratio Over---------------------------->\n");

    /**
     * Last number filter
     */
    printf("\n\n<------------------------------------Filter Last Number------------------------------>\n");
    printf("#################################### INFORMATIONS ##################################\n");
    printf("#     This step is Last Number filter. This operation will find the SAME number of #\n");
    printf("# the last number of every number in stages before. To do this, you will going to  #\n");
    printf("# do two things, the same as last step. One, choose filter number, Two, the same   #\n");
    printf("# number of mantissa.      ^ ^                                                     #\n");
    printf("#################################### INFORMATIONS ##################################\n\n");

    printf("Same number of mantissa of stages before is as follows:\n");
    // show a table.
    showSameNumberOfMantissa();
    printf("Now input the number you want to filter ALTOGETHER(1-5):");
    while(1) {
        scanf("%d", &MantissaSameNumberN);
        if((MantissaSameNumberN>5) || (MantissaSameNumberN<0)) {
            printf("You have entered invalid number.Enter again(1-5):");
        } else {
            break;
        }
    }
    arrMantissaSameNumber = (int *)malloc(MantissaSameNumberN*sizeof(int));
    printf("Now input, you have choosed %d mantissa same numbers.\n", MantissaSameNumberN);
    for(i=0; i<MantissaSameNumberN; i++) {
        while(1){
            printf("MantissaSameNumber%d:",i+1);
            scanf("%d", &arrMantissaSameNumber[i]);
            if((arrMantissaSameNumber[i]>5) || (arrMantissaSameNumber[i]<0)) {
                printf("You have entered invalid number. Enter again(1-5):");
            } else {
                break;
            }
        }
    }
    for(i=0; i<MantissaSameNumberN; i++) {
        if(i==0)
            printf("Your choosed mantissa same numbers are:\n");
        printf("%d ", arrMantissaSameNumber[i]);
    }
    printf("\n");
    printf("Filtering...\n");

    // new list for save groups.
    GroupListAfterFilterMantissaSameNumber = NULL;
    p2 = (GroupList *)malloc(sizeof(GroupList));
    // Delete the cache. you know that.
    DestroyP = GroupListAfterFilterParityRatio;
    tempj = 0;
    while(GroupListAfterFilterParityRatio != NULL) {
        MantissaSameNumberRet = getParityRatio(GroupListAfterFilterParityRatio);
        showProcessI++;
        showProcessBar(showProcessI, showProcessJ);
        for(i=0; i<MantissaSameNumberN; i++) {
            if(MantissaSameNumberRet == arrMantissaSameNumber[i]) {
                tempj++;
                p1 = (GroupList *)malloc(sizeof(GroupList));
                for(j=0; j<7; j++) {
                    p1->Ball[j] = GroupListAfterFilterParityRatio->Ball[j];
                }
                p1->next = NULL;
                if(GroupListAfterFilterMantissaSameNumber == NULL) {
                    GroupListAfterFilterMantissaSameNumber = p1;
                } else {
                    p2->next = p1;
                }
                p2 = p1;
                break;
            }
        }
        GroupListAfterFilterParityRatio = GroupListAfterFilterParityRatio->next;
    }
    showProcessJ = tempj;
    showProcessI = 0;
    printf("Delete Caches.\n");
    if (DestroyP == NULL) {
        printf("There mayby a error set DestroyP value before, please check.\n");
        exit(1);
    } else {
        while(DestroyP->next != NULL) {
            p1 = DestroyP->next;
            free(DestroyP);
            DestroyP = p1;
        }
    }
    GroupListAfterFilterParityRatio = NULL;
    printf("Filter mantissa same numbers over. There are still %d groups left.\n", tempj);
    printf("<----------------------------------Filter Last Number Over---------------------------->\n");

    printf("\n\n<--------------------------------------Filter The Sum-------------------------------->\n");
    printf("#################################### INFORMATIONS ##################################\n");
    printf("#     This step is called filter the sum. As the name, this step will filter the   #\n");
    printf("# gourp by one group's sum(without blue ball). I seperate the sum value in 20 part #\n");
    printf("# (1-10, 11-20, 21-30.....191-200), So you would better to choose more than two pa #\n");
    printf("# rt if you want a better filter.  ^ ^                                             #\n");
    printf("#################################### INFORMATIONS ##################################\n\n");
    Sleep(500);

    // show sum table.
    showSum();

    printf("Now input the number you want to filter ALTOGETHER(1-20):");
    while(1) {
        scanf("%d", &sumChooseN);
        if((sumChooseN>20) || (sumChooseN<0)) {
            printf("You have entered invalid number.Enter again(1-20):");
        } else {
            break;
        }
    }
    arrSumValuechoose = (int *)malloc(sumChooseN*sizeof(int));
    printf("Now input, you have choosed %d sum choose numbers.\n", sumChooseN);
    printf(" 0:  1- 10,  1: 11- 20,  2: 21- 30,  3: 31- 40,  4: 41- 50,  5: 51- 60,  6: 61- 70,  7: 71- 80,\n");
    printf(" 8: 81- 90,  9: 91-100, 10:101-110, 11:111-120, 12:121-130, 13:131-140, 14:141-150, 15:151-160,\n");
    printf("16:161-170, 17:171-180, 18:181-190, 19:191-200\n");
    for(i=0; i<sumChooseN; i++) {
        while(1){
            printf("sumChooseN%d:",i+1);
            scanf("%d", &arrSumValuechoose[i]);
            if((arrSumValuechoose[i]>20) || (arrSumValuechoose[i]<0)) {
                printf("You have entered invalid number. Enter again(1-20):");
            } else {
                break;
            }
        }
    }
    for(i=0; i<sumChooseN; i++) {
        if(i==0)
            printf("Your choosed sums are:\n");
        printf("%d ", arrSumValuechoose[i]);
    }
    printf("\n");
    printf("Filtering...\n");

    // new list for save groups.
    GroupListAfterFilterSumValue = NULL;
    p2 = (GroupList *)malloc(sizeof(GroupList));
    // Delete the cache. you know that.
    DestroyP = GroupListAfterFilterMantissaSameNumber;
    tempj = 0;
    while(GroupListAfterFilterMantissaSameNumber != NULL) {
        sumValueRet = getSum(GroupListAfterFilterMantissaSameNumber);
        showProcessI++;
        showProcessBar(sumChooseN, showProcessJ);
        for(i=0; i<MantissaSameNumberN; i++) {
            if(sumValueRet == arrSumValuechoose[i]) {
                tempj++;
                p1 = (GroupList *)malloc(sizeof(GroupList));
                for(j=0; j<7; j++) {
                    p1->Ball[j] = GroupListAfterFilterMantissaSameNumber->Ball[j];
                }
                p1->next = NULL;
                if(GroupListAfterFilterSumValue == NULL) {
                    GroupListAfterFilterSumValue = p1;
                } else {
                    p2->next = p1;
                }
                p2 = p1;
                break;
            }
        }
        GroupListAfterFilterMantissaSameNumber = GroupListAfterFilterMantissaSameNumber->next;
    }
    showProcessJ = tempj;
    showProcessI = 0;
    printf("Delete Caches.\n");
    if (DestroyP == NULL) {
        printf("There mayby a error set DestroyP value before, please check.\n");
        exit(1);
    } else {
        while(DestroyP->next != NULL) {
            p1 = DestroyP->next;
            free(DestroyP);
            DestroyP = p1;
        }
    }
    GroupListAfterFilterMantissaSameNumber = NULL;
    printf("Filter Sum value numbers over. There are still %d groups left.\n", tempj);
    printf("<-----------------------------------Filter The Sum Over----------------------------->\n");


    printf("\n\n<---------------------------------Filter Consecutive Number--------------------------->\n");

    printf("#################################### INFORMATIONS ##################################\n");
    printf("#     This step called Consecutive number filter, this is to say numbers consecuti #\n");
    printf("# ve numbers will be filtered in this step. As you can calculate, the biggest cons #\n");
    printf("# ecutive number without blue ball is five, and this is the limit number in my pro #\n");
    printf("# gramme. So, let's do it!    ^ ^                                                  #\n");
    printf("#################################### INFORMATIONS ##################################\n\n");

    // show consecutive number table
    showConsecutiveNumber();

    printf("Now input the number you want to filter ALTOGETHER(0-5):");
    while(1) {
        scanf("%d", &consecutiveNumberChooseN);
        if((consecutiveNumberChooseN>5) || (consecutiveNumberChooseN<0)) {
            printf("You have entered invalid number.Enter again(0-5):");
        } else {
            break;
        }
    }
    arrConsecutiveNumber = (int *)malloc(consecutiveNumberChooseN*sizeof(int));
    printf("Now input, you have choosed %d consecutive numbers.\n", consecutiveNumberChooseN);
    for(i=0; i<consecutiveNumberChooseN; i++) {
        while(1){
            printf("consecutiveNumberChooseN%d:",i+1);
            scanf("%d", &arrConsecutiveNumber[i]);
            if((arrConsecutiveNumber[i]>5) || (arrConsecutiveNumber[i]<0)) {
                printf("You have entered invalid number. Enteragain (0-5):");
            } else {
                break;
            }
        }
    }
    for(i=0; i<consecutiveNumberChooseN; i++) {
        if(i==0)
            printf("Your choosed consecutive numbers are:\n");
        printf("%d ", arrConsecutiveNumber[i]);
    }
    printf("\n");
    printf("Filtering...\n");

    // new list for save groups.
    GroupListAfterFilterConsecutiveNumber = NULL;
    p2 = (GroupList *)malloc(sizeof(GroupList));
    // Delete the cache. you know that.
    DestroyP = GroupListAfterFilterSumValue;
    tempj = 0;
    while(GroupListAfterFilterSumValue != NULL) {
        consecutiveNumberRet = getConsecutiveNumber(GroupListAfterFilterSumValue);
        showProcessI++;
        showProcessBar(showProcessI, showProcessJ);
        for(i=0; i<consecutiveNumberChooseN; i++) {
            if(consecutiveNumberRet == arrConsecutiveNumber[i]) {
                tempj++;
                p1 = (GroupList *)malloc(sizeof(GroupList));
                for(j=0; j<7; j++) {
                    p1->Ball[j] = GroupListAfterFilterSumValue->Ball[j];
                }
                p1->next = NULL;
                if(GroupListAfterFilterConsecutiveNumber == NULL) {
                    GroupListAfterFilterConsecutiveNumber = p1;
                } else {
                    p2->next = p1;
                }
                p2 = p1;
                break;
            }
        }
        GroupListAfterFilterSumValue = GroupListAfterFilterSumValue->next;
    }
    showProcessJ = tempj;
    showProcessI = 0;
    printf("Delete Caches.\n");
    if (DestroyP == NULL) {
        printf("There mayby a error set DestroyP value before, please check.\n");
        exit(1);
    } else {
        while(DestroyP->next != NULL) {
            p1 = DestroyP->next;
            free(DestroyP);
            DestroyP = p1;
        }
    }
    GroupListAfterFilterSumValue = NULL;
    printf("Filter consecutive number over. There are still %d groups left.\n", tempj);
    printf("<-------------------------------Filter Consecutive Number Over------------------------->\n");


    printf("\n\n<---------------------------------------Filter Re NO.--------------------------------->\n");
    printf("#################################### INFORMATIONS ##################################\n");
    printf("#     Re no. filter: Find how many re no. between this stage and last stage. You   #\n");
    printf("# can thoose how many re no. to filter, just according to the table below. This st #\n");
    printf("# ep is easy just enter one number, and you will get it.  ^ ^                      #\n");
    printf("#################################### INFORMATIONS ##################################\n");

    // show re no. tale.
    showReNo();

    printf("Now input the number you want to filter ALTOGETHER(0-6):");
    while(1) {
        scanf("%d", &reNochooseN);
        if((reNochooseN>5) || (reNochooseN<0)) {
            printf("You have entered invalid number.Enter again(0-6):");
        } else {
            break;
        }
    }
    arrReNo = (int *)malloc(reNochooseN*sizeof(int));
    printf("Now input, you have choosed %d re no.\n", reNochooseN);
    printf("0 is 0 re No; 1 is 1 re No; 2 is 2 re No....\n");
    for(i=0; i<reNochooseN; i++) {
        while(1){
            printf("reNochooseN%d:",i+1);
            scanf("%d", &arrReNo[i]);
            if((arrReNo[i]>6) || (arrReNo[i]<0)) {
                printf("You have entered invalid number. Enteragain (0-6):");
            } else {
                break;
            }
        }
    }
    for(i=0; i<reNochooseN; i++) {
        if(i==0)
            printf("Your choosed re no.s are:\n");
        printf("%d ", arrReNo[i]);
    }
    printf("\n");
    printf("Filtering...\n");

    // new list for save groups.
    GroupListAfterFilterReNo = NULL;
    p2 = (GroupList *)malloc(sizeof(GroupList));
    // Delete the cache. you know that.
    DestroyP = GroupListAfterFilterConsecutiveNumber;
    tempj = 0;
    // set previous list
    listPre = *GroupListAfterFilterConsecutiveNumber;
    GroupListAfterFilterConsecutiveNumber = GroupListAfterFilterConsecutiveNumber->next;
    while(GroupListAfterFilterConsecutiveNumber != NULL) {
        reNoRet = getReNo(listPre, *GroupListAfterFilterConsecutiveNumber);
        showProcessI++;
        showProcessBar(showProcessI, showProcessJ);
        for(i=0; i<reNochooseN; i++) {
            if(reNoRet == arrReNo[i]) {
                tempj++;
                p1 = (GroupList *)malloc(sizeof(GroupList));
                for(j=0; j<7; j++) {
                    p1->Ball[j] = GroupListAfterFilterConsecutiveNumber->Ball[j];
                }
                p1->next = NULL;
                if(GroupListAfterFilterReNo == NULL) {
                    GroupListAfterFilterReNo = p1;
                } else {
                    p2->next = p1;
                }
                p2 = p1;
                break;
            }
        }
        listPre = *GroupListAfterFilterConsecutiveNumber;
        GroupListAfterFilterConsecutiveNumber = GroupListAfterFilterConsecutiveNumber->next;
    }
    showProcessJ = tempj;
    showProcessI = 0;
    printf("Delete Caches.\n");
    if (DestroyP == NULL) {
        printf("There mayby a error set DestroyP value before, please check.\n");
        exit(1);
    } else {
        while(DestroyP->next != NULL) {
            p1 = DestroyP->next;
            free(DestroyP);
            DestroyP = p1;
        }
    }
    GroupListAfterFilterConsecutiveNumber = NULL;
    printf("Filter re No. over. There are still %d groups left.\n", tempj);
    printf("<-------------------------------------Filter Re NO. Over------------------------------->\n");


    printf("\n\n<-------------------------------------Filter Big to Samll------------------------------->\n");
    printf("#################################### INFORMATIONS ##################################\n");
    printf("#     This step is to calculate one gourp numbers' size ratio. One is bigger than  #\n");
    printf("# 17 is called big number, as samller than 17 called small number. And there a thr #\n");
    printf("# ee situations: 33(three big, three small) 42(four big, two small)...So, just ent #\n");
    printf("# er you filter request, and just do it.            ^ ^                            #\n");
    printf("#################################### INFORMATIONS ##################################\n");

    // show size ratio table.
    showBigToSmall();

    printf("Now input the number you want to filter ALTOGETHER(1-7):");
    while(1) {
        scanf("%d", &sizeRatioN);
        if((sizeRatioN>7) || (sizeRatioN<0)) {
            printf("You have entered invalid number.Enter again(1-7):");
        } else {
            break;
        }
    }
    arrSizeRatio = (int *)malloc(sizeRatioN*sizeof(int));
    printf("Now input, you have choosed %d size ratio numbers.\n", sizeRatioN);
    printf("0(3:3) 1(4:2) 2(2:4) 3(5:1) 4(1:5) 5(6:0) 6(0:6)\n");
    for(i=0; i<sizeRatioN; i++) {
        while(1){
            printf("sizeRatioN%d:",i+1);
            scanf("%d", &arrSizeRatio[i]);
            if((arrSizeRatio[i]>7) || (arrSizeRatio[i]<0)) {
                printf("You have entered invalid number. Enteragain (1-7):");
            } else {
                break;
            }
        }
    }
    for(i=0; i<sizeRatioN; i++) {
        if(i==0)
            printf("Your choosed size ratio numbers are:\n");
        printf("%d ", arrSizeRatio[i]);
    }
    printf("\n");
    printf("Filtering...\n");

    // new list for save groups.
    GroupListAfterFilterSizeRatio = NULL;
    p2 = (GroupList *)malloc(sizeof(GroupList));
    // Delete the cache. you know that.
    DestroyP = GroupListAfterFilterReNo;
    tempj = 0;
    while(GroupListAfterFilterReNo != NULL) {
        sizeRatioRet = getBigToSmall(GroupListAfterFilterReNo);
        showProcessI++;
        showProcessBar(showProcessI, showProcessJ);
        for(i=0; i<sizeRatioN; i++) {
            if(sizeRatioRet == arrSizeRatio[i]) {
                tempj++;
                p1 = (GroupList *)malloc(sizeof(GroupList));
                for(j=0; j<7; j++) {
                    p1->Ball[j] = GroupListAfterFilterReNo->Ball[j];
                }
                p1->next = NULL;
                if(GroupListAfterFilterSizeRatio == NULL) {
                    GroupListAfterFilterSizeRatio = p1;
                } else {
                    p2->next = p1;
                }
                p2 = p1;
                break;
            }
        }
        GroupListAfterFilterReNo = GroupListAfterFilterReNo->next;
    }
    showProcessJ = tempj;
    showProcessI = 0;
    printf("Delete Caches.\n");
    if (DestroyP == NULL) {
        printf("There mayby a error set DestroyP value before, please check.\n");
        exit(1);
    } else {
        while(DestroyP->next != NULL) {
            p1 = DestroyP->next;
            free(DestroyP);
            DestroyP = p1;
        }
    }
    GroupListAfterFilterSumValue = NULL;
    printf("Filter size ratio over. There are still %d groups left.\n", tempj);
    printf("<-----------------------------------Filter Big to Samll Over----------------------------->\n");

    /**
     * Maxinum re-Number filter.
     */
    printf("\n\n<-----------------------------------Filter Maxinum Re-Number----------------------------->\n");
    printf("#################################### INFORMATIONS ##################################\n");
    printf("#     This step is to calculate one gourp numbers' Maxinum re-Number with other st #\n");
    printf("# ages before. More infomation: select one group from all stages before to compare #\n");
    printf("# the numbers, calculate the maxinum re-Numbers in the numbers. As you can see the #\n");
    printf("# following table, Number 4 is the most maxinum of all.     ^ ^                    #\n");
    printf("#################################### INFORMATIONS ##################################\n");

    // show tables.
    showMaxinumReNumber();

    printf("Now input the number you want to filter ALTOGETHER(1-7):");
    while(1) {
        scanf("%d", &maxinumReNumberN);
        if((maxinumReNumberN>7) || (maxinumReNumberN<0)) {
            printf("You have entered invalid number.Enter again(1-7):");
        } else {
            break;
        }
    }
    arrMaxinumReNumber = (int *)malloc(maxinumReNumberN*sizeof(int));
    printf("Now input, you have choosed %d Maxinum Re- numbers.\n", maxinumReNumberN);
    for(i=0; i<maxinumReNumberN; i++) {
        while(1){
            printf("maxinumReNumberN%d:",i+1);
            scanf("%d", &arrMaxinumReNumber[i]);
            if((arrMaxinumReNumber[i]>7) || (arrMaxinumReNumber[i]<0)) {
                printf("You have entered invalid number. Enteragain (1-7):");
            } else {
                break;
            }
        }
    }
    for(i=0; i<maxinumReNumberN; i++) {
        if(i==0)
            printf("Your choosed Maxinum Re- numbers are:\n");
        printf("%d ", arrMaxinumReNumber[i]);
    }
    printf("\n");
    printf("Filtering...\n");

    // write to file end.
    time(&curTime);
    timeInfo = localtime(&curTime);
    printf("%4d-%02d-%02d %02d:%02d:%02d\n",1900+timeInfo->tm_year, 1+timeInfo->tm_mon,
                                            timeInfo->tm_mday,timeInfo->tm_hour,timeInfo->tm_min,timeInfo->tm_sec);
    sprintf(saveTime, "%02d-%02d-%02d-%02d-", 1+timeInfo->tm_mon, timeInfo->tm_mday,timeInfo->tm_hour,timeInfo->tm_min);
    strcat(saveTime, "GroupList");
    if((fileP = OpenAFile(saveTime, "a+")) == NULL) {
        exit(1);
    }
    fprintf(fileP, "------List start------\n");
    // new list for save groups.
    GroupListAfterFilterMaxinumReNumber = NULL;
    p2 = (GroupList *)malloc(sizeof(GroupList));
    // Delete the cache. you know that.
    DestroyP = GroupListAfterFilterSizeRatio;
    tempj = 0;
    while(GroupListAfterFilterSizeRatio != NULL) {
        MaxinumReNumberRet = getMaxinumReNumber(GroupListAfterFilterSizeRatio, 0);
        showProcessI++;
        showProcessBar(showProcessI, showProcessJ);
        for(i=0; i<maxinumReNumberN; i++) {
            if(MaxinumReNumberRet == arrMaxinumReNumber[i]) {
                tempj++;
                p1 = (GroupList *)malloc(sizeof(GroupList));
                for(j=0; j<7; j++) {
                    p1->Ball[j] = GroupListAfterFilterSizeRatio->Ball[j];
                    fprintf(fileP, "%02d ", p1->Ball[j]);
                }
                fprintf(fileP,"\n");
                p1->next = NULL;
                if(GroupListAfterFilterMaxinumReNumber == NULL) {
                    GroupListAfterFilterMaxinumReNumber = p1;
                } else {
                    p2->next = p1;
                }
                p2 = p1;
                break;
            }
        }
        GroupListAfterFilterSizeRatio = GroupListAfterFilterSizeRatio->next;
    }
    fprintf(fileP, "------List End------\n");
    showProcessJ = tempj;
    showProcessI = 0;
    printf("Delete Caches.\n");
    if (DestroyP == NULL) {
        printf("There mayby a error set DestroyP value before, please check.\n");
        exit(1);
    } else {
        while(DestroyP->next != NULL) {
            p1 = DestroyP->next;
            free(DestroyP);
            DestroyP = p1;
        }
    }
    fclose(fileP);
    GroupListAfterFilterSizeRatio = NULL;
    printf("Filter size ratio over. There are still %d groups left.\n", tempj);
    printf("<-------------------------------------Filter Maxinum Re-Number Over------------------------------->\n");

    system("pause");


}

/**
 * Calulate factorial.
 */
U16 factorialAll(U16 a, U16 b)
{
    U16 i, j;
    U16 factorialRet, factorialA = 1;
    U16 factorialB = 1;

    for(i=a; i>(a-b); i--)
    {
        factorialA = factorialA * i;
    }
    //printf("factorialA : %d\n", factorialA);
    while(b)
    {
        factorialB = factorialB * b;
        b--;
    }
    //printf("factorialB : %d\n", factorialB);

    factorialRet = factorialA/factorialB;

    return factorialRet;
}

/**
 * GenerateHotColdNumber(): Generate hot and cold numbers.
 */
void GenerateHotColdNumber()
{
    FILE *LuckyNumberFileP;
    struct OneStageNumbers* ArrNum;
    int ArrNumSave[8], ArrHotNumberSave[8], ArrColdNumberSave[8];
    int i, j, tempMinimize, tempMaxinum, minimizeI, maxinumI; // temp variable

    printf("Generating hot and cold numbers.\n");
    // Init the HotColdNumberN Array.
    for(i=0; i<33; i++)
        HotColdNumberN[i] = 0;
    // Init the ArrHotColdNumberSave Array.
    for(i=0; i<8; i++) {
        ArrHotNumberSave[i] = 0;
        ArrColdNumberSave[i] = 0;
    }

    if ((LuckyNumberFileP = OpenAFile("LuckyNumbers.txt", "a+")) == NULL) {
        exit(1);
    }
    tempMinimize = 0;
    minimizeI = 0;
    tempMaxinum = 0;
    maxinumI = 0;
    while(!feof(LuckyNumberFileP)) {
        ArrNum = ReadOneLineFromFile(LuckyNumberFileP);
        for(i=0; i<8; i++) {
            // Make a temp array is to save the data. Because after ReadOneLineFromFile function
            // is over, the memory of ArrNum is gone.
            ArrNumSave[i] = ArrNum->Num[i];
        }
        printf("%d,%d,%d,%d,%d,%d,%d,%d\r", ArrNumSave[0], ArrNumSave[1], ArrNumSave[2], ArrNumSave[3],
                                        ArrNumSave[4], ArrNumSave[5], ArrNumSave[6], ArrNumSave[7]);
        for(i=0; i<33; i++) {
            for(j=1; j<7; j++) {
                if (ArrNumSave[j] == (i+1)) {
                    HotColdNumberN[i]++;
                }
            }
        }
    }
    //printf("           No.:");
    //for(i=0; i<33; i++) {
    //    printf("%03d ", i+1);
    //}
    //printf("\n");
    //printf("HotColdNumberN:");
    //for(i=0; i<33; i++) {
    //    printf("%d ", HotColdNumberN[i]);
    //}
    //printf("\n");
    /**
     * Get front eight hot number and last eight cold number.
     */
    for(i=0; i<8; i++) { // set default eight number is 1-8.
        ArrHotNumberSave[i] = i;
        ArrColdNumberSave[i] = i;
    }
    for(i=8; i<33; i++) { // judge 8-33 numbers one by one for HOT numbers
        tempMinimize = HotColdNumberN[ArrHotNumberSave[0]];
        minimizeI = 0;
        //printf("%d:Current maxinum is :", i+1);
        //for(j=0; j<8; j++) {
        //    printf("%d ", HotColdNumberN[ArrHotNumberSave[j]]);
        //}
        //printf("\n");
        for(j=1; j<8; j++) { // every number(one of 8-33) will be compared by eight numbers which is in ArrHotColdNumberSave array.
            if (HotColdNumberN[ArrHotNumberSave[j]] < tempMinimize) {
                tempMinimize = HotColdNumberN[ArrHotNumberSave[j]];
                minimizeI = j;
            }
        }
        //printf("Current minimize number: %d. minimizeI: %d, going to campare is %d: %d\n", tempMinimize, minimizeI+1, i+1, HotColdNumberN[i]);

        //printf("%d >? %d\n", HotColdNumberN[i], HotColdNumberN[ArrHotNumberSave[minimizeI]]);
        if ( HotColdNumberN[i] > HotColdNumberN[ArrHotNumberSave[minimizeI]] ) {
            //printf("%d->", ArrHotNumberSave[minimizeI]+1);
            ArrHotNumberSave[minimizeI] = i;
            //printf("%d\n", i+1);
        }

        //printf("New maxinum is :", i+1);
        //for(j=0; j<8; j++) {
        //    printf("%d ", HotColdNumberN[ArrHotNumberSave[j]]);
        //}
        //printf("\n\n");

    }
    for(i=8; i<33; i++) { // judge 8-33 numbers one by one for COLD numbers
        tempMaxinum = HotColdNumberN[ArrColdNumberSave[0]];
        maxinumI = 0;
        //printf("%d:Current minimize is :", i+1);
        //for(j=0; j<8; j++) {
        //    printf("%d ", HotColdNumberN[ArrColdNumberSave[j]]);
        //}
        //printf("\n");
        for(j=1; j<8; j++) { // every number(one of 8-33) will be compared by eight numbers which is in ArrHotColdNumberSave array.
            if (HotColdNumberN[ArrColdNumberSave[j]] > tempMaxinum) {
                tempMaxinum = HotColdNumberN[ArrColdNumberSave[j]];
                maxinumI = j;
            }
        }
        //printf("Current tempMaxinum number: %d. maxinumI: %d, going to campare is %d: %d\n", tempMaxinum, maxinumI+1, i+1, HotColdNumberN[i]);

        //printf("%d <? %d\n", HotColdNumberN[i], HotColdNumberN[ArrColdNumberSave[maxinumI]]);
        if ( HotColdNumberN[i] < HotColdNumberN[ArrColdNumberSave[maxinumI]] ) {
            //printf("%d->", ArrColdNumberSave[maxinumI]+1);
            ArrColdNumberSave[maxinumI] = i;
            //printf("%d\n", i+1);
        }

        //printf("New minimize is :");
        //for(j=0; j<8; j++) {
        //    printf("%d ", HotColdNumberN[ArrColdNumberSave[j]]);
        //}
        //printf("\n\n");

    }
    for(i=0; i<8; i++) {
        if (i == 0)
            printf("Hot numbers:");
        printf("%d, ", ArrHotNumberSave[i]+1);
        if (i == 7)
            printf("\n");
    }
    for(i=0; i<8; i++) {
        if (i == 0)
            printf("Cold numbers:");
        printf("%d, ", ArrColdNumberSave[i]+1);
        if (i == 7)
            printf("\n");
    }

    // malloc and set hot cold numbers' value , remember to free them.
    HotNumber = (int *)malloc(8*sizeof(int));
    ColdNumber = (int *)malloc(8*sizeof(int));

    for(i=0; i<8; i++) {
        HotNumber[i] = ArrHotNumberSave[i];
        ColdNumber[i] = ArrColdNumberSave[i];
    }

    fclose(LuckyNumberFileP);

}

/**
 * GenerateAllGroup(): Generate All possible groups.
 */
int GenerateAllGroup(U16 redN, U16 blueN)
{
    int i1, i2, i3, i4, i5, i6;
    int j1;
    int GroupI;
    U16 GroupN, redFactorialN, blueFactorialN;

    /**
     * Calculate all group numbers.
     */
    redFactorialN = factorialAll(redN, 6);
    blueFactorialN = factorialAll(blueN, 1);
    GroupN = redFactorialN * blueFactorialN;
    printf("There are 33 red balls.\n");
    printf("And 16 blue balls.\n");
    printf("So there are %d Groups.\n", GroupN);

    /**
     * Init the ArrAllGroup array.
     */
    printf("\nInit the Array...\n");
    ArrAllGroup = (int *)malloc(GroupN*sizeof(int *));
    // ArrAllGroup:
    // Stage, red1, red2, red3, red4, red5, red6, blue, range
    for(GroupI=0; GroupI<GroupN; GroupI++)
        ArrAllGroup[GroupI] = malloc(7*sizeof(int));
    //printf("The size of the Array is :%d\n", sizeof(ArrAllGroup));

    /**
     * Plus 1 is to be more understandable
     */
    redN += 1;
    blueN += 1;

    printf("Generating groups, it may take a while, please wait...\n");
    GroupI = 0;
    for(i1=1; i1<redN; i1++)
    {
        for(i2=(i1+1); i2<redN; i2++)
        {
            if (i2 == i1) continue;
            for(i3=(i2+1); i3<redN; i3++)
            {
                if ((i3 == i2) || (i3 == i1)) continue;
                for(i4=(i3+1); i4<redN; i4++)
                {
                    if ((i4 == i3) || (i4 == i2) || (i4 == i1)) continue;
                    for(i5=(i4+1); i5<redN; i5++)
                    {
                        if ((i5 == i4) || (i5 == i3) || (i5 == i2) || (i5 == i1)) continue;
                        for(i6=(i5+1); i6<redN; i6++)
                        {
                            if ((i6 == i5) || (i6 == i4) || (i6 == i3) || (i6 == i2) || (i6 == i1)) continue;
                            for(j1=1; j1<blueN; j1++)
                            {
                                ArrAllGroup[GroupI][0] = i1;
                                ArrAllGroup[GroupI][1] = i2;
                                ArrAllGroup[GroupI][2] = i3;
                                ArrAllGroup[GroupI][3] = i4;
                                ArrAllGroup[GroupI][4] = i5;
                                ArrAllGroup[GroupI][5] = i6;
                                ArrAllGroup[GroupI][6] = j1;
                                //printf("ArrAllgroup[%d]:%d %d %d %d %d %d, %d\n", GroupI, ArrAllGroup[GroupI][0], ArrAllGroup[GroupI][1],
                                //                                                ArrAllGroup[GroupI][2],ArrAllGroup[GroupI][3],
                                //                                                ArrAllGroup[GroupI][4],ArrAllGroup[GroupI][5],ArrAllGroup[GroupI][6]);

                                // calculate current group's number range(without blue ball).
                                //ArrAllGroup[GroupI][7] = NumberRangeCalculate(ArrAllGroup[GroupI][0], ArrAllGroup[GroupI][1], ArrAllGroup[GroupI][2],
                                //                                              ArrAllGroup[GroupI][3], ArrAllGroup[GroupI][4], ArrAllGroup[GroupI][5]);
                                // calculate current gourp's difference between two near number(without blue ball).

                                showProcessBar(GroupI, GroupN);

                                GroupI++;
                            }
                        }
                    }
                }
            }
        }
    }
    printf("%d groups have inited over.\n", GroupI);
    return GroupN;
}





